package com.sh.data.engine.domain.workspace.manager.model.domain;

import com.sh.data.engine.common.enumDefinition.DSType;
import com.sh.data.engine.domain.integration.datasource.model.param.DataSourceHiveSaveParam;
import com.sh.data.engine.domain.integration.datasource.model.param.DataSourceRdbmsSaveParam;
import com.sh.data.engine.domain.integration.datasource.model.param.DataSourceSaveParam;
import lombok.Data;

import java.util.Date;

/**
 * @author: mengzheng.mhc
 * @date: 2024/6/12 16:25
 */
@Data
public class StorageDetailDomain {

    private Long id;

//    private String projectName;

    private String storageName;

    private String storageType;

    private String storageDesc;

    private String server;

    private String version;

    private Integer port;

    private String dbName;

    private String username;

    private String password;

    private String connectionProperty;

    private String storageLink;

    private String schema;

    private Integer status;

    private String originalFilename;

    private String hiveSiteAddress;

    public DataSourceSaveParam convertToDataSourceSaveParam() {

        String systemcDataSourcePre = "系统默认_";
        // need projectId
        // support other rdbms
        if (DSType.isRdbms(storageType)) {
            var dataSourceParam = new DataSourceRdbmsSaveParam();
            dataSourceParam.setPassword(password);
            dataSourceParam.setDbName(dbName);
            dataSourceParam.setPort(port);
            dataSourceParam.setServer(server);
            dataSourceParam.setConnectionProperty(connectionProperty);
            dataSourceParam.setDsLink(storageLink);
            dataSourceParam.setDsType(storageType);
            dataSourceParam.setRowState(1);
            dataSourceParam.setIsSystem(1);
            dataSourceParam.setUsername(username);
            dataSourceParam.setDsName(systemcDataSourcePre + storageName);
            dataSourceParam.setStatus(status);
            dataSourceParam.setVersion(version);
            dataSourceParam.setDsDesc(storageDesc);
            dataSourceParam.setSchema(schema);
            dataSourceParam.setUpdateTime(new Date());
            dataSourceParam.setCreateTime(new Date());
            if (DSType.PostgreSQL.name().equalsIgnoreCase(storageType)) {
                dataSourceParam.setSchema(schema);
            } else if (DSType.KINGBASE8.name().equalsIgnoreCase(storageType)) {
                dataSourceParam.setSchema(schema);
            }
            return dataSourceParam;
        } else if (DSType.Hive.name().equalsIgnoreCase(storageType)) {
            var dataSourceParam = new DataSourceHiveSaveParam();
            dataSourceParam.setPassword(password);
            dataSourceParam.setDbName(dbName);
            dataSourceParam.setHiveSiteAddress(hiveSiteAddress);
            dataSourceParam.setPort(port);
            dataSourceParam.setServer(server);
            dataSourceParam.setConnectionProperty(connectionProperty);
            dataSourceParam.setDsLink(storageLink);
            dataSourceParam.setDsType(storageType);
            dataSourceParam.setRowState(1);
            dataSourceParam.setOriginalFilename(originalFilename);
            dataSourceParam.setIsSystem(1);
            dataSourceParam.setStatus(status);
            dataSourceParam.setVersion(version);
            dataSourceParam.setUsername(username);
            dataSourceParam.setDsDesc(storageDesc);
            dataSourceParam.setDsName(systemcDataSourcePre + storageName);
            dataSourceParam.setUpdateTime(new Date());
            dataSourceParam.setCreateTime(new Date());
            return dataSourceParam;
        }
        return null;
    }
}
